﻿using Sidvall.Data;
using Sidvall.Security;

namespace Sidvall.DataPortal
{
    public static class ServerConnectionManager
    {
        #region GetFileStorageServerConnection

        public static ServerConnection GetFileStorageServerConnection(string path, string key, string description, string storageManagerFactoryTypeName,
            string transactionScopeFactoryTypeName)
        {
            var storageManagerFactory = Sidvall.SystemContext.Current.CodeManager.CreateInstance(storageManagerFactoryTypeName) as Sidvall.Data.IStorageManagerFactory;
            var transactionScopeFactory = Sidvall.SystemContext.Current.CodeManager.CreateInstance(transactionScopeFactoryTypeName) as Sidvall.Data.ITransactionScopeFactory;
            return GetFileStorageServerConnection(path, key, description, storageManagerFactory, transactionScopeFactory);
        }
        public static ServerConnection GetFileStorageServerConnection(string path, string key, string description, IStorageManagerFactory storageManagerFactory,
            ITransactionScopeFactory transactionScopeFactory)
        {
            var item = new ServerConnection()
            {
                ConnectionTypeId = ConnectionType.FileStorage,
                Key = key,
                Description = description,
                StorageManagerFactory = storageManagerFactory,
                TransactionScopeFactory = transactionScopeFactory,
                ConnectionString = path,
            };
            return item;
        }

        #endregion
        #region GetSqliteConnection

        public static ServerConnection GetSqliteConnection(string filePath, string key, string description, string storageManagerFactoryTypeName,
            string transactionScopeFactoryTypeName)
        {
            var storageManagerFactory = Sidvall.SystemContext.Current.CodeManager.CreateInstance(storageManagerFactoryTypeName) as Sidvall.Data.IStorageManagerFactory;
            var transactionScopeFactory = Sidvall.SystemContext.Current.CodeManager.CreateInstance(transactionScopeFactoryTypeName) as Sidvall.Data.ITransactionScopeFactory;
            return GetSqliteConnection(filePath, key, description, storageManagerFactory, transactionScopeFactory);
        }
        public static ServerConnection GetSqliteConnection(string filePath, string key, string description, IStorageManagerFactory storageManagerFactory,
            ITransactionScopeFactory transactionScopeFactory)
        {
            var item = new ServerConnection()
            {
                ConnectionTypeId = ConnectionType.LocalSqlite,
                Key = key,
                Description = description,
                StorageManagerFactory = storageManagerFactory,
                TransactionScopeFactory = transactionScopeFactory,
                ConnectionString = filePath,
            };
            return item;
        }

        #endregion
        #region GetSqlServerConnection

        public static ServerConnection GetSqlServerConnection(string connectionString, string storageManagerAssembly, string key, string description,
            string storageManagerFactoryTypeName, string transactionScopeFactoryTypeName)
        {
            var storageManagerFactory = Sidvall.SystemContext.Current.CodeManager.CreateInstance(storageManagerFactoryTypeName) as Sidvall.Data.IStorageManagerFactory;
            var transactionScopeFactory = Sidvall.SystemContext.Current.CodeManager.CreateInstance(transactionScopeFactoryTypeName) as Sidvall.Data.ITransactionScopeFactory;
            return GetSqlServerConnection(connectionString, storageManagerAssembly, key, description, storageManagerFactory, transactionScopeFactory);
        }
        public static ServerConnection GetSqlServerConnection(string connectionString, string storageManagerAssembly, string key, string description,
            IStorageManagerFactory storageManagerFactory, ITransactionScopeFactory transactionScopeFactory)
        {
            var item = new ServerConnection()
            {
                ConnectionTypeId = ConnectionType.NetworkSqlServer,
                Key = key,
                Description = description,
                StorageManagerFactory = storageManagerFactory,
                TransactionScopeFactory = transactionScopeFactory,
                ConnectionString = GetEntityConnectionString(storageManagerAssembly, connectionString),
            };
            return item;
        }

        #endregion
        #region GetWebServiceConnection

        public static ServerConnection GetWebServiceConnection(string address, string key, string description, IAuthenticationStrategy authenticationStrategy)
        {
            var item = new ServerConnection()
            {
                ConnectionTypeId = ConnectionType.RemoteAccess,
                Key = key,
                Description = description,
                RemoteAddress = address,
                AuthenticationStrategy = authenticationStrategy,
            };
            return item;
        }

        #endregion

        #region GetEntityConnectionString

        public static string GetEntityConnectionString(string storageManagerAssembly, string connectionString)
        {
            if (string.IsNullOrWhiteSpace(storageManagerAssembly))
                return null;
            if (string.IsNullOrWhiteSpace(connectionString))
                return null;
            var sb = new System.Text.StringBuilder();
            sb.Append(@"metadata=res://" + storageManagerAssembly + "/Model.Model.csdl|res://"
                + storageManagerAssembly + "/Model.Model.ssdl|res://" + storageManagerAssembly
                + @"/Model.Model.msl;provider=System.Data.SqlClient;provider connection string=""");
            sb.Append(connectionString.TrimEnd(';') + ";");
            sb.Append("MultipleActiveResultSets=True;App=EntityFramework;");
            sb.Append("\"");
            return sb.ToString();
        }

        #endregion
    }
}
